package com.linkage.educloud.ah.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.linkage.educloud.ah.R;
import com.zzhou.common.library.util.StringUtil;


public class CommonDialog extends Dialog {

    private String title;

    private String contentText;
    private BaseAdapter mAdpater;
    private OnItemClickListener mItemClickListener;

    private String cancel;
    private String ok;
    private View.OnClickListener cancelListener;
    private View.OnClickListener okListener;

    public CommonDialog(Context context) {
        super(context, R.style.MyDialogStyleBottom);
    }

    public CommonDialog(Context context, String title, String contentText,
                        String cancel, String ok) {
        this(context);
        this.title = title;
        this.contentText = contentText;
        this.cancel = cancel;
        this.ok = ok;
    }

    public CommonDialog(Context context, String title,
                        BaseAdapter mAdpater, OnItemClickListener mItemClickListener,
                        String cancel, String ok) {
        this(context);
        this.title = title;
        this.contentText = null;
        this.mAdpater = mAdpater;
        this.mItemClickListener = mItemClickListener;
        this.cancel = cancel;
        this.ok = ok;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_dialog_common);
        TextView titleTextView = (TextView) findViewById(R.id.title);
        TextView contentTextView = (TextView) findViewById(R.id.contentTextView);
        ListView contentListView = (ListView) findViewById(R.id.contentListView);
        Button cancelButton = (Button) findViewById(R.id.cancel);
        Button okButton = (Button) findViewById(R.id.ok);
        titleTextView.setText(title);
        if (contentText != null) {
            contentListView.setVisibility(View.GONE);
            contentTextView.setVisibility(View.VISIBLE);
            contentTextView.setText(contentText);
        } else {
            contentTextView.setVisibility(View.GONE);
            contentListView.setVisibility(View.VISIBLE);
            contentListView.setAdapter(mAdpater);
            contentListView.setOnItemClickListener(mItemClickListener);
        }
        if (getCancelListener() == null || StringUtil.isNullOrEmpty(cancel)) {
            cancelButton.setVisibility(View.GONE);
            // sureButton.setVisibility(View.VISIBLE);
        }else{
            cancelButton.setVisibility(View.VISIBLE);
            cancelButton.setText(cancel);
        }
        if (StringUtil.isNullOrEmpty(ok)) {
            okButton.setVisibility(View.GONE);
            okButton.setText(ok);
        }else{
            okButton.setVisibility(View.VISIBLE);
            okButton.setText(ok);
        }
        cancelButton.setOnClickListener(getCancelListener());
        okButton.setOnClickListener(getOkListener());
        setCanceledOnTouchOutside(true );
    }

    public View.OnClickListener getCancelListener() {
        return cancelListener;
    }

    public void setCancelListener(View.OnClickListener cancelListener) {
        this.cancelListener = cancelListener;
    }

    public View.OnClickListener getOkListener() {
        return okListener;
    }

    public void setOkListener(View.OnClickListener okListener) {
        this.okListener = okListener;
    }

}
